/**
 * 删除所有会议记录脚本
 * 清空所有会议、参会者、转录、摘要数据
 */

const Database = require('better-sqlite3');
const path = require('path');

// 数据库路径
const dbPath = path.join(__dirname, 'packages', 'server', 'data', 'database.sqlite');

console.log('📂 数据库路径:', dbPath);

try {
  // 打开数据库
  const db = new Database(dbPath);
  
  // 临时禁用外键约束
  console.log('\n🔓 临时禁用外键约束...');
  db.pragma('foreign_keys = OFF');
  
  // 查询当前会议数量
  const totalMeetings = db.prepare('SELECT COUNT(*) as count FROM meetings').get();
  const totalParticipants = db.prepare('SELECT COUNT(*) as count FROM meeting_participants').get();
  const totalTranscripts = db.prepare('SELECT COUNT(*) as count FROM meeting_transcripts').get();
  const totalSummaries = db.prepare('SELECT COUNT(*) as count FROM meeting_summaries').get();
  
  console.log('\n📊 当前数据统计:');
  console.log(`  会议: ${totalMeetings.count}`);
  console.log(`  参会者: ${totalParticipants.count}`);
  console.log(`  转录: ${totalTranscripts.count}`);
  console.log(`  摘要: ${totalSummaries.count}`);
  
  if (totalMeetings.count === 0) {
    console.log('\n✅ 没有需要删除的会议！');
    db.pragma('foreign_keys = ON');
    db.close();
    process.exit(0);
  }
  
  console.log('\n🗑️ 开始删除所有会议数据...');
  
  // 删除所有数据（外键约束已禁用）
  const transcriptsResult = db.prepare('DELETE FROM meeting_transcripts').run();
  console.log(`  ✅ 删除转录: ${transcriptsResult.changes} 条`);
  
  const summariesResult = db.prepare('DELETE FROM meeting_summaries').run();
  console.log(`  ✅ 删除摘要: ${summariesResult.changes} 条`);
  
  const participantsResult = db.prepare('DELETE FROM meeting_participants').run();
  console.log(`  ✅ 删除参会者: ${participantsResult.changes} 条`);
  
  const meetingsResult = db.prepare('DELETE FROM meetings').run();
  console.log(`  ✅ 删除会议: ${meetingsResult.changes} 条`);
  
  // 重新启用外键约束
  console.log('\n🔒 重新启用外键约束...');
  db.pragma('foreign_keys = ON');
  
  // 验证删除
  const remainingMeetings = db.prepare('SELECT COUNT(*) as count FROM meetings').get();
  const remainingParticipants = db.prepare('SELECT COUNT(*) as count FROM meeting_participants').get();
  
  console.log('\n📊 删除后数据统计:');
  console.log(`  会议: ${remainingMeetings.count}`);
  console.log(`  参会者: ${remainingParticipants.count}`);
  
  // 关闭数据库
  db.close();
  
  console.log('\n🎉 完成！所有会议记录已清空。');
  console.log('💡 提示: 刷新浏览器的会议历史页面，应该显示"暂无会议记录"');
  
} catch (error) {
  console.error('❌ 错误:', error.message);
  console.error('详细错误:', error);
  process.exit(1);
}

